{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Performance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Profile Decoder\n",
    "\n",
    "The default Python and Cython decoder can be profiled with Python's standard `cprofile`. The output can be a sorted table and a flame graph. Both are generated below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "python -m openpifpaf.predict coco/000000081988.jpg --no-download-progress --debug --profile-decoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!flameprof profile_decoder.prof > profile_decoder_flame.svg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![flame](profile_decoder_flame.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is a second output that is generated from the Autograd Profiler. This can only be viewed in the Chrome browser: \n",
    "* open `chrome://tracing`\n",
    "* click \"Load\" in the top left corner\n",
    "* select `decoder_profile.1.json`\n",
    "\n",
    "This is the same type of plot that is used to trace the training of a batch. An example of such a plot is shown below.\n",
    "\n",
    "\n",
    "## Profile Training\n",
    "\n",
    "For a training batch, the Chrome trace looks like this:\n",
    "\n",
    "![train_trace](images/chrome_trace_train.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
